#-------------------------------------------------------------------------------
# Name:        cube_perser
# Purpose:
#
# Author:      Ryosuke Mondo
#
# Created:     31/07/2011
# Copyright:   (c) m 2011
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python

# conv tbl. When converted a turn single layer to dowble layer,
# it has need to convert the rest turns to adjust.
l_dic =  { "U":"B", "F":"U",          "D":"F", "B":"D"          }
ll_dic = { "U":"D", "F":"B",          "D":"U", "B":"F"          }
lp_dic = { "U":"F", "F":"D",          "D":"B", "B":"U"          }
b_dic =  { "U":"R",          "L":"U", "D":"L",          "R":"D" }
bb_dic = { "U":"D",          "L":"R", "D":"U",          "R":"L" }
bp_dic = { "U":"L",          "L":"D", "D":"R",          "R":"U" }
d_dic =  {          "F":"L", "L":"B",          "B":"R" ,"R":"F" }
dd_dic = {          "F":"B", "L":"R",          "B":"F" ,"R":"L" }
dp_dic = {          "F":"R", "L":"F",          "B":"L", "R":"B" }

# for select corresponding table.
conv_dic = { "L":l_dic, "L'":lp_dic, "L2":ll_dic,
             "B":b_dic, "B'":bp_dic, "B2":bb_dic,
             "D":d_dic, "D'":dp_dic, "D2":dd_dic }

# anable to convert L, B, and D.
replace_dic = { 'L':'Rw', 'B':'Fw','D':'Uw' }

def main():
    file_in = "manuvers.txt"
    file_out = open( 'out.txt', 'w')

    turns = input

    for algs in open( file_in, 'r'):
        turns = algs[:-1].split( " " )      # split with space ( ex. "L", "B'" ..)
        turns_len = len( turns )

        for loop in range( turns_len ):
            if( turns[loop] == "" ):
                break
            turns = conv_alg( loop, turns ) # convert single to double layered turn.

        line_out = ""
        for item in turns:
            line_out += item + " "
        file_out.write( line_out + "\n" )   # write a line.
    file_out.close()

def conv_alg( loop, turns ):
    conv_key = turns[loop][0]
    conv_value = replace_dic.get( conv_key )
    if( conv_value ):                                                           # If key is L, B, or D then true.
        current_dic = conv_dic.get( turns[loop] )                               # select conv table.
        turns[loop] = turns[loop].replace( conv_key, conv_value )               # conv single to double
        loop += 1
        while( turns[loop] <> "" ):                                             # single to double conv effects rest turns.
            conv_turn = current_dic.get( turns[loop][0] )
            if( conv_turn ):                                                    # if has key. It means need to conv.
                turns[loop] = turns[loop].replace( turns[loop][0], conv_turn )
            loop += 1
    return turns

if __name__ == '__main__':
    main()
